//
//  StockEdit.m
//  iHedron
//
//  Created by George Radev on 2011-07-22.
//  Copyright 2011 I Me Mine. All rights reserved.
//

#import "StockEdit.h"
#import "TextEditCell.h"
#import "HedronCore.h"

@implementation StockEdit

@synthesize stockId, stockName, stockNameField;

#pragma mark -
#pragma mark View lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
	if(stockId>0){
		self.title= @"Редактиране Стока";
	}else{
		self.title= @"Нова Стока";
	}	
	
	UIBarButtonItem* rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Запиши" 
					style:UIBarButtonItemStyleDone target:self action:@selector(doSaveStock)];
	self.navigationItem.rightBarButtonItem = rightButton;
	[rightButton release];
}

-(void)doSaveStock{
	NSString * stockfieldname = stockNameField.text;
	if([stockfieldname length] > 0){
		if(stockId>0){
			[HedronCore updateStock:stockId name:stockfieldname];
		}else{
			[HedronCore newStock:stockfieldname];
		}	
		
		[self.navigationController popViewControllerAnimated:TRUE];
		
	}else {
		UIAlertView *alert = [[UIAlertView alloc] init];
		[alert setTitle:@"Грешка"];
		[alert setMessage:@"Моля, попълнете име за стока"];
		[alert setDelegate:self];
		[alert addButtonWithTitle:@"Ок"];
		[alert show];
		[alert release];
	}
}

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
*/
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return 1;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"TextEditCell";
    
    TextEditCell *cell = (TextEditCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
		NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"TextEditCell" owner:nil options:nil];
		
		for(id currentObject in topLevelObjects){
			if([currentObject isKindOfClass:[UITableViewCell class]]){
				cell = (TextEditCell*) currentObject;
				break;
			}
		}
    }
    
    // Configure the cell...
	if(cell){
		cell.textField.text = stockName;
		stockNameField = cell.textField;
		[stockNameField becomeFirstResponder];
	}
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UILabel* headerLabel = [[UILabel alloc] init];
    headerLabel.frame = CGRectMake(10, 0, 300, 40);
    headerLabel.backgroundColor = [UIColor clearColor];
    //headerLabel.textColor = [UIColor blackColor];
    //headerLabel.font = [UIFont boldSystemFontOfSize:18];
    headerLabel.text = @"   Име на стока";
	[headerLabel autorelease];
    return headerLabel;
}


#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
    /*
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
    // ...
    // Pass the selected object to the new view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
    [detailViewController release];
    */
}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

